package org.loofer.uikit;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.drawable.RotateDrawable;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;

public class DataListFooterView extends TextView {
    private RotateDrawable mDrawable;
    private ObjectAnimator mObjectAnimator;

    public DataListFooterView(Context context) {
        this(context, null);
    }

    public DataListFooterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews(context);
    }

    public DataListFooterView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
        initViews(context);
    }

    private void initViews(Context context) {
        setPadding(0, (int) getResources().getDimension(R.dimen.footer_padding), 0, (int) getResources().getDimension(R.dimen.footer_padding));
        setTextSize(12.0f);
        this.mDrawable = (RotateDrawable) context.getResources().getDrawable(R.drawable.xml_footer_refresh);
        this.mDrawable.setBounds(0, 0, this.mDrawable.getIntrinsicWidth(), this.mDrawable.getIntrinsicHeight());
        setGravity(17);
        setVisibility(8);
        this.mObjectAnimator = ObjectAnimator.ofInt(this.mDrawable, "level", new int[]{0, 10000});
        this.mObjectAnimator.setDuration(1000);
        this.mObjectAnimator.setRepeatCount(-1);
        this.mObjectAnimator.setInterpolator(new LinearInterpolator());
    }

    public void showLoadingAnim() {
        setVisibility(0);
        setText(R.string.loading);
        setCompoundDrawablesWithIntrinsicBounds(null, this.mDrawable, null, null);
        this.mObjectAnimator.start();
    }

    public void showLastPageText(CharSequence text) {
        setVisibility(0);
        clearRefreshDrawable();
        setText(text);
        setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
    }

    public void showLoadFailText() {
        setVisibility(0);
        clearRefreshDrawable();
        setText(R.string.load_failed);
        setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
    }

    public void hide() {
        setVisibility(4);
        clearRefreshDrawable();
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        clearRefreshDrawable();
    }

    private void clearRefreshDrawable() {
        this.mObjectAnimator.cancel();
    }
}
